package org.aeon.client.view.components;

import org.aeon.client.context.GlobalContext;
import org.aeon.client.view.components.listColumn.ListColumnPanel;
import org.aeon.client.view.components.menuPanel.MenuPanel;
import org.aeon.client.view.components.sessionPanel.SessionPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;

/**
 * @author: MissingAeon
 * @date: 2024/2/29 9:02
 * @description: 应用程序分层面板
 */
public class ApplicationLayeredPanel extends JLayeredPane {

    public ApplicationLayeredPanel() {

        JPanel defaultLayer = new JPanel();
        // 盒子布局
        defaultLayer.setLayout(new BoxLayout(defaultLayer, BoxLayout.X_AXIS));
        defaultLayer.setBounds(0, 0, 1067, 693);

        final SessionPanel sessionPanel = new SessionPanel();
        final ListColumnPanel listColumnPanel = new ListColumnPanel(sessionPanel);
        GlobalContext.getContext().setListColumnPanel(listColumnPanel);
        defaultLayer.add(new MenuPanel(listColumnPanel, sessionPanel));
        defaultLayer.add(listColumnPanel);
        defaultLayer.add(sessionPanel);
        // 默认层
        this.add(defaultLayer, JLayeredPane.DEFAULT_LAYER);

    }

    public void addPopUpLayer(Component component) {
        component.addMouseListener(new MouseAdapter() {
            // 加入鼠标事件，防止事件穿透
        });
        this.add(component, JLayeredPane.POPUP_LAYER);
        this.setLayer(component, JLayeredPane.POPUP_LAYER, 0);
        revalidate();
        repaint();
    }

    public void addPopUpLayer(Component component, int position) {
        component.addMouseListener(new MouseAdapter() {
            // 加入鼠标事件，防止事件穿透
        });
        this.add(component, JLayeredPane.POPUP_LAYER);
        this.setLayer(component, JLayeredPane.POPUP_LAYER, position);
        revalidate();
        repaint();

        component.requestFocus();
    }

    public void removePopUpLayer(Component component) {
        this.remove(component);
        revalidate();
        repaint();
    }
}
